package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.WeiLiuServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "weiliu")
public class WeiLiuAdaptor implements IAdaptor {

	private static Logger log = Logger.getLogger(WeiLiuAdaptor.class);

	public void order() {
		
	    OrderBean orderBean = OrderInfoContext.getContext();
	    orderBean.setSendTime(new Date());
	    String pkgNo = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String tradeNo = orderBean.getIds();//订购流水号
		String region  = orderBean.getRegion();// 0 - 全国     1 - 省
		
		
		String result = WeiLiuServiceImpl.service.order(pkgNo,phoneNo,tradeNo,region);
		log.info("between- 微流WeiLiuService.order() 订购请求result<>:" + result);
	
		
		JSONObject json = JSONObject.fromObject(result);

		String Code = json.getString("Code");//状态码

		if ("0".equals(Code)) {
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("Message"));
			orderBean.setErrorMsg(json.getString("Message"));
		} else {
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("Message"));
			orderBean.setErrorMsg(json.getString("Message"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	public void status() {
	

		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo = orderBean.getIds();

		String result = WeiLiuServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between- 微流WeiLiuService.orderStatus() 查询响应response<> :" + result);
	
		JSONObject json = JSONObject.fromObject(result);

		String resultCode = json.getString("resultCode");

		if ("0".equals(resultCode)) {
			String orderSate = json.getString("orderSate");
			if ("001".equals(orderSate)||"002".equals(orderSate)||"003".equals(orderSate)) {
			
				orderBean.setStatus("1");
				orderBean.setMessage("订单在受理中");
				orderBean.setErrorMsg("订单在受理中");
			}else if ("009".equals(orderSate)) {
				orderBean.setStatus("7");
				orderBean.setMessage("充值成功");
				orderBean.setErrorMsg("充值成功");
				
			}else {
				orderBean.setStatus("8");
				orderBean.setMessage("充值失败");
				orderBean.setErrorMsg("充值失败");
			}
			
		}else{
			orderBean.setStatus("1");
			orderBean.setMessage("订单在受理中");
			orderBean.setErrorMsg("订单在受理中");
		} 
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
	
	
}
